<%--
  Created by IntelliJ IDEA.
  User: gubler
  Date: 2022/1/12
  Time: 17；08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" type="text/javascript"></script>
</head>
<body>

<form>
    <p>
        <label>User Name:</label>
        <input type="text" id="userName" value="sun"/>
    </p>
    <p>
        <label>Password:</label>
        <input type="password" id="password" value="123" />
    </p>
    <p>
        <input type="button" id="sub" value="Login" />
        <input type="button" id="register" value="register" onclick="jump()"/>
    </p>
</form>
<script>
    function jump(){
        window.location.href = "/register"
    }
    $(function() {
        $("#sub").bind("click", function() {
            login();
        })
    });

    function login() {
        // 获取页面输入框的值，包装到 user js 对象中
        var user = {};
        user.userName = $("#userName").val();
        user.password = $("#password").val();
        if (user.userName == "" || user.password == "") {
            alert("Please input user name and password.");
            return;
        }
        // 调用 ajax 请求后台的 login 控制器，处理响应结果
        $.ajax({
            url: "/api/login",
            type:"post",
            contentType: "application/json",
            data:JSON.stringify(user),
            success: function(result) {
                if (result.status == 200) {
                    window.location.href = "/dashboard";
                } else  {
                    alert(result.message);
                }
            },
            error:function (data) {
                alert(data);
            }
        });
    }

</script>
</body>
</html>


